package com.example.thirdhomework

import android.content.Intent
import android.os.Bundle
import android.provider.MediaStore
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import androidx.activity.result.ActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {

    private val getResult = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result: ActivityResult ->
        if (result.resultCode == RESULT_OK) {
            val data = result.data?.getStringExtra("RESULT_KEY")
            findViewById<TextView>(R.id.resultTextView).text = data
        }
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        findViewById<Button>(R.id.button1).setOnClickListener { onButton1Click() }
        findViewById<Button>(R.id.button2).setOnClickListener { onButton2Click() }
        findViewById<Button>(R.id.button3).setOnClickListener { onButton3Click() }
        findViewById<Button>(R.id.button4).setOnClickListener { onButton4Click() }
    }
    private fun onButton1Click() {
        val editText: EditText = findViewById(R.id.editTextText)
        val inputText = editText.text.toString()
        val intent = Intent(this, SecondActivity::class.java).apply {
            putExtra("EXTRA_MESSAGE", inputText)
        }
        startActivity(intent)
    }
    private fun onButton2Click() {
        val intent = Intent(this, ThirdActivity::class.java)
        getResult.launch(intent)
    }
    private fun onButton3Click() {
        // 启动相机
        val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
        getResult.launch(intent) // 使用新API
    }

    private fun onButton4Click() {
        val intent = Intent(this, StopWatchActivity::class.java)
        startActivity(intent)
    }
}
